home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
pyxmpp
/
jabber
/
muc.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
17KB
|
507 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
__revision__ = '$Id: muc.py 647 2006-08-26 18:27:39Z jajcus $'
__docformat__ = 'restructuredtext en'
import logging
from pyxmpp.presence import Presence
from pyxmpp.message import Message
from pyxmpp.iq import Iq
from pyxmpp.jid import JID
from pyxmpp.xmlextra import xml_element_ns_iter
from pyxmpp.jabber.muccore import MucPresence, MucUserX, MucItem, MucStatus
from pyxmpp.jabber.muccore import MUC_OWNER_NS
from pyxmpp.jabber.dataforms import DATAFORM_NS, Form
import weakref
class MucRoomHandler:
def __init__(self):
self.room_state = None
self._MucRoomHandler__logger = logging.getLogger('pyxmpp.jabber.MucRoomHandler')
def assign_state(self, state_obj):
self.room_state = state_obj
def room_created(self, stanza):
_unused = stanza
self.room_state.request_instant_room()
def configuration_form_received(self, form):
pass
def room_configured(self):
pass
def user_joined(self, user, stanza):
pass
def user_left(self, user, stanza):
pass
def role_changed(self, user, old_role, new_role, stanza):
pass
def affiliation_changed(self, user, old_aff, new_aff, stanza):
pass
def nick_change(self, user, new_nick, stanza):
pass
def nick_changed(self, user, old_nick, stanza):
pass
def presence_changed(self, user, stanza):
pass
def subject_changed(self, user, stanza):
pass
def message_received(self, user, stanza):
pass
def room_configuration_error(self, stanza):
self.error(stanza)
def error(self, stanza):
err = stanza.get_error()
self._MucRoomHandler__logger.debug('Error from: %r Condition: %r' % (stanza.get_from(), err.get_condition))
class MucRoomUser:
def __init__(self, presence_or_user_or_jid):
if isinstance(presence_or_user_or_jid, MucRoomUser):
self.presence = presence_or_user_or_jid.presence
self.role = presence_or_user_or_jid.role
self.affiliation = presence_or_user_or_jid.affiliation
self.room_jid = presence_or_user_or_jid.room_jid
self.real_jid = presence_or_user_or_jid.real_jid
self.nick = presence_or_user_or_jid.nick
self.new_nick = None
else:
self.affiliation = 'none'
self.presence = None
self.real_jid = None
self.new_nick = None
if isinstance(presence_or_user_or_jid, JID):
self.nick = presence_or_user_or_jid.resource
self.room_jid = presence_or_user_or_jid
self.role = 'none'
elif isinstance(presence_or_user_or_jid, Presence):
self.nick = None
self.room_jid = None
self.role = 'participant'
self.update_presence(presence_or_user_or_jid)
else:
raise TypeError, 'Bad argument type for MucRoomUser constructor'
def update_presence(self, presence):
self.presence = MucPresence(presence)
t = presence.get_type()
if t == 'unavailable':
self.role = 'none'
self.affiliation = 'none'
self.room_jid = self.presence.get_from()
self.nick = self.room_jid.resource
mc = self.presence.get_muc_child()
if isinstance(mc, MucUserX):
items = mc.get_items()
for item in items:
if not isinstance(item, MucItem):
continue
if item.role:
self.role = item.role
if item.affiliation:
self.affiliation = item.affiliation
if item.jid:
self.real_jid = item.jid
if item.nick:
self.new_nick = item.nick
break
def same_as(self, other):
return self.room_jid == other.room_jid
class MucRoomState:
def __init__(self, manager, own_jid, room_jid, handler):
self.own_jid = own_jid
self.room_jid = room_jid
self.handler = handler
self.manager = weakref.proxy(manager)
self.joined = False
self.subject = None
self.users = { }
self.me = MucRoomUser(room_jid)
self.configured = None
self.configuration_form = None
handler.assign_state(self)
self._MucRoomState__logger = logging.getLogger('pyxmpp.jabber.MucRoomState')
def get_user(self, nick_or_jid, create = False):
if isinstance(nick_or_jid, JID):
if not nick_or_jid.resource:
return None
for u in self.users.values():
if nick_or_jid in (u.room_jid, u.real_jid):
return u
continue
if create:
return MucRoomUser(nick_or_jid)
else:
return None
return self.users.get(nick_or_jid)
def set_stream(self, stream):
_unused = stream
if self.joined and self.handler:
self.handler.user_left(self.me, None)
self.joined = False
def join(self, password = None, history_maxchars = None, history_maxstanzas = None, history_seconds = None, history_since = None):
if self.joined:
raise RuntimeError, 'Room is already joined'
p = MucPresence(to_jid = self.room_jid)
p.make_join_request(password, history_maxchars, history_maxstanzas, history_seconds, history_since)
self.manager.stream.send(p)
def leave(self):
if self.joined:
p = MucPresence(to_jid = self.room_jid, stanza_type = 'unavailable')
self.manager.stream.send(p)
def send_message(self, body):
m = Message(to_jid = self.room_jid.bare(), stanza_type = 'groupchat', body = body)
self.manager.stream.send(m)
def set_subject(self, subject):
m = Message(to_jid = self.room_jid.bare(), stanza_type = 'groupchat', subject = subject)
self.manager.stream.send(m)
def change_nick(self, new_nick):
new_room_jid = JID(self.room_jid.node, self.room_jid.domain, new_nick)
p = Presence(to_jid = new_room_jid)
self.manager.stream.send(p)
def get_room_jid(self, nick = None):
if nick is None:
return self.room_jid
return JID(self.room_jid.node, self.room_jid.domain, nick)
def get_nick(self):
return self.room_jid.resource
def process_available_presence(self, stanza):
fr = stanza.get_from()
if not fr.resource:
return None
nick = fr.resource
user = self.users.get(nick)
if user:
old_user = MucRoomUser(user)
user.update_presence(stanza)
user.nick = nick
else:
old_user = None
user = MucRoomUser(stanza)
self.users[user.nick] = user
self.handler.presence_changed(user, stanza)
if fr == self.room_jid and not (self.joined):
self.joined = True
self.me = user
mc = stanza.get_muc_child()
if self.configured is None:
self.configured = True
if not old_user or old_user.role == 'none':
self.handler.user_joined(user, stanza)
elif old_user.nick != user.nick:
self.handler.nick_changed(user, old_user.nick, stanza)
if old_user.room_jid == self.room_jid:
self.room_jid = fr
if old_user.role != user.role:
self.handler.role_changed(user, old_user.role, user.role, stanza)
if old_user.affiliation != user.affiliation:
self.handler.affiliation_changed(user, old_user.affiliation, user.affiliation, stanza)
def process_unavailable_presence(self, stanza):
fr = stanza.get_from()
if not fr.resource:
return None
nick = fr.resource
user = self.users.get(nick)
if user:
old_user = MucRoomUser(user)
user.update_presence(stanza)
self.handler.presence_changed(user, stanza)
if user.new_nick:
mc = stanza.get_muc_child()
if isinstance(mc, MucUserX):
renames = _[1]
if renames:
self.users[user.new_nick] = user
del self.users[nick]
return None
else:
old_user = None
user = MucRoomUser(stanza)
self.users[user.nick] = user
self.handler.presence_changed(user, stanza)
if fr == self.room_jid and self.joined:
self.joined = False
self.handler.user_left(user, stanza)
self.manager.forget(self)
self.me = user
elif old_user:
self.handler.user_left(user, stanza)
def process_groupchat_message(self, stanza):
fr = stanza.get_from()
user = self.get_user(fr, True)
s = stanza.get_subject()
if s:
self.subject = s
self.handler.subject_changed(user, stanza)
else:
self.handler.message_received(user, stanza)
def process_error_message(self, stanza):
self.handler.error(stanza)
def process_error_presence(self, stanza):
self.handler.error(stanza)
def process_configuration_form_success(self, stanza):
if stanza.get_query_ns() != MUC_OWNER_NS:
raise ValueError, 'Bad result namespace'
query = stanza.get_query()
form = None
for el in xml_element_ns_iter(query.children, DATAFORM_NS):
form = Form(el)
break
if not form:
raise ValueError, 'No form received'
self.configuration_form = form
self.handler.configuration_form_received(form)
def process_configuration_form_error(self, stanza):
self.handler.error(stanza)
def request_configuration_form(self):
iq = Iq(to_jid = self.room_jid.bare(), stanza_type = 'get')
iq.new_query(MUC_OWNER_NS, 'query')
self.manager.stream.set_response_handlers(iq, self.process_configuration_form_success, self.process_configuration_form_error)
self.manager.stream.send(iq)
return iq.get_id()
def process_configuration_success(self, stanza):
_unused = stanza
self.configured = True
self.handler.room_configured()
def process_configuration_error(self, stanza):
self.handler.room_configuration_error(stanza)
def configure_room(self, form):
if form.type == 'cancel':
return None
elif form.type != 'submit':
raise ValueError, "A 'submit' form required to configure a room"
iq = Iq(to_jid = self.room_jid.bare(), stanza_type = 'set')
query = iq.new_query(MUC_OWNER_NS, 'query')
form.as_xml(query)
self.manager.stream.set_response_handlers(iq, self.process_configuration_success, self.process_configuration_error)
self.manager.stream.send(iq)
return iq.get_id()
def request_instant_room(self):
if self.configured:
raise RuntimeError, 'Instant room may be requested for unconfigured room only'
form = Form('submit')
return self.configure_room(form)
class MucRoomManager:
def __init__(self, stream):
self.rooms = { }
(self.stream, self.jid) = (None, None)
self.set_stream(stream)
self._MucRoomManager__logger = logging.getLogger('pyxmpp.jabber.MucRoomManager')
def set_stream(self, stream):
self.jid = stream.me
self.stream = stream
for r in self.rooms.values():
r.set_stream(stream)
def set_handlers(self, priority = 10):
self.stream.set_message_handler('groupchat', self._MucRoomManager__groupchat_message, None, priority)
self.stream.set_message_handler('error', self._MucRoomManager__error_message, None, priority)
self.stream.set_presence_handler('available', self._MucRoomManager__presence_available, None, priority)
self.stream.set_presence_handler('unavailable', self._MucRoomManager__presence_unavailable, None, priority)
self.stream.set_presence_handler('error', self._MucRoomManager__presence_error, None, priority)
def join(self, room, nick, handler, password = None, history_maxchars = None, history_maxstanzas = None, history_seconds = None, history_since = None):
if not (room.node) or room.resource:
raise ValueError, 'Invalid room JID'
room_jid = JID(room.node, room.domain, nick)
cur_rs = self.rooms.get(room_jid.bare().as_unicode())
if cur_rs and cur_rs.joined:
raise RuntimeError, 'Room already joined'
rs = MucRoomState(self, self.stream.me, room_jid, handler)
self.rooms[room_jid.bare().as_unicode()] = rs
rs.join(password, history_maxchars, history_maxstanzas, history_seconds, history_since)
return rs
def get_room_state(self, room):
return self.rooms.get(room.bare().as_unicode())
def forget(self, rs):
try:
del self.rooms[rs.room_jid.bare().as_unicode()]
except KeyError:
pass
def __groupchat_message(self, stanza):
fr = stanza.get_from()
key = fr.bare().as_unicode()
rs = self.rooms.get(key)
if not rs:
self._MucRoomManager__logger.debug('groupchat message from unknown source')
return False
rs.process_groupchat_message(stanza)
return True
def __error_message(self, stanza):
fr = stanza.get_from()
key = fr.bare().as_unicode()
rs = self.rooms.get(key)
if not rs:
return False
rs.process_error_message(stanza)
return True
def __presence_error(self, stanza):
fr = stanza.get_from()
key = fr.bare().as_unicode()
rs = self.rooms.get(key)
if not rs:
return False
rs.process_error_presence(stanza)
return True
def __presence_available(self, stanza):
fr = stanza.get_from()
key = fr.bare().as_unicode()
rs = self.rooms.get(key)
if not rs:
return False
rs.process_available_presence(MucPresence(stanza))
return True
def __presence_unavailable(self, stanza):
fr = stanza.get_from()
key = fr.bare().as_unicode()
rs = self.rooms.get(key)
if not rs:
return False
rs.process_unavailable_presence(MucPresence(stanza))
return True